/*
 * Copyright 2010 Gal Dolber.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.unnison.framework.client.ga;

import com.google.gwt.core.client.GWT;
import com.google.inject.Inject;

import com.unnison.framework.client.GuitController;
import com.unnison.framework.client.binder.EventBusHandler;
import com.unnison.framework.client.binder.GuitBinder;
import com.unnison.framework.client.binder.useraction.event.UserActionEvent;
import com.unnison.framework.client.ga.GoogleAnalyticsTracker.GoogleAnalyticsUserActionTrackerBinder;
import com.unnison.framework.client.place.Place;
import com.unnison.framework.client.place.event.PlaceGoEvent;
import com.unnison.framework.client.place.event.PlaceNewItemEvent;

public class GoogleAnalyticsTracker extends GuitController<GoogleAnalyticsUserActionTrackerBinder> {

    public static interface GoogleAnalyticsUserActionTrackerBinder extends GuitBinder<GoogleAnalyticsTracker> {
    }

    private final GoogleAnalytics util;

    @Inject
    public GoogleAnalyticsTracker(GoogleAnalytics util) {
        this.util = util;
    }

    /**
     * Listen for place changes.
     */
    @EventBusHandler(PlaceGoEvent.class)
    protected <D> void $placeGo(Class<? extends Place<D>> placeClass, D placeData, String token) {
        trackPageView(placeClass, placeData, token);
    }

    /**
     * Listen for place changes.
     */
    @EventBusHandler(PlaceNewItemEvent.class)
    protected <D> void $placeNewItem(Class<? extends Place<D>> placeClass, D placeData, String token) {
        trackPageView(placeClass, placeData, token);
    }

    /**
     * Listen for user actions.
     */
    @EventBusHandler(UserActionEvent.class)
    protected void $userAction(String category, String userAction, String text, Integer number) {
        if (GWT.isScript()) {
            if (text == null && number == null) {
                util.trackEvent(category, userAction);
            } else {
                util.trackEvent(category, userAction, text, number);
            }
        }
    }

    /**
     * Set account.
     */
    public void setAccount(String ua) {
        if (GWT.isScript()) {
            util.setAccount(ua);
        }
    }

    private <D> void trackPageView(Class<? extends Place<D>> placeClass, D placeData, String token) {
        if (token == null) {
            token = placeManager.getToken(placeClass, placeData);
        }
        if (GWT.isScript()) {
            util.trackPageview(GWT.getModuleBaseURL() + token);
        }
    }
}
